package chapter08

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test06_Function
 * Package: chapter08
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 *          Date: 2023-07-01
 *          Time: 11:15
 */

object Test06_Function {
  def main(args: Array[String]): Unit = {
    val list: List[(String, Int)] = List(("a", 12), ("b", 35), ("c", 27), ("a", 11))

    //1.元组做一个转换处理 实现key不变 val2倍
    val newList = list.map(tp => (tp._1, tp._2 * 2))
    //2.模式匹配实现
    val newList2 = list.map(
      tp => {
        tp match {
          case (word, count) => (word, count * 2)
        }
      }
    )
    //省略lambda表达式 进行简化 形成了偏函数
    val newList3 = list.map {
      case (word, count) => (word, count * 2)
    }

    println(newList)
    println(newList2)
    println(newList3)
    //偏函数应用 求绝对值
    //对输出数据 分为不同的情形 正负0
    val postAbs:PartialFunction[Int,Int] = {
      case x if x > 0 => x
    }
    val negAbs:PartialFunction[Int,Int] = {
      case x if x < 0 => -x
    }
    val zeroAbs:PartialFunction[Int,Int] = {
      case 0 => 0
    }
    //讲偏函数组合到一起
    def abs(x:Int):Int = (postAbs orElse negAbs orElse zeroAbs) (x)
    println(abs(-45))
    println(abs(0))
    println(abs(99))





  }

}
